﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Catering.View_Operaction;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class staff_lessons_finish_list : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.LoadDrop();
            }
        }

        protected void LoadDrop()
        {
            string userid = Session["PersonnelID"].ToString();
            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_ZuoYeQu, isAdmin, groupid, companyid, false);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_ZuoYeQu.SelectedItem, Drop_BanZu, isAdmin, classid, false);

            if (Session["RBGropuID"] != null) this.Drop_ZuoYeQu.SelectedValue = Session["RBGropuID"].ToString();
            if (Session["RBClassID"] != null) this.Drop_BanZu.SelectedValue = Session["RBClassID"].ToString();
            if (!isAdmin)
            {
                this.Drop_ZuoYeQu.Enabled = false;
                this.Drop_BanZu.Enabled = false;
            }

            Session.Remove("RBGropuID");
            Session.Remove("RBClassID");
        }

        protected void Drop_ZuoYeQu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");
            if (Drop_BanZu.SelectedItem != null)
            {
                Session["ClassID"] = Drop_BanZu.SelectedValue;
                Session["ClassName"] = Drop_BanZu.SelectedItem.Text;
            }
            if (Drop_BanZu.SelectedItem != null)
            {
                Session["GroupID"] = Drop_ZuoYeQu.SelectedValue;
                Session["GroupName"] = Drop_ZuoYeQu.SelectedItem.Text;
            }
        }

        //公用的DropDownList绑定方法
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }

        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Drop_BanZu.SelectedItem != null)
            {
                Session["ClassID"] = Drop_BanZu.SelectedValue;
                Session["ClassName"] = Drop_BanZu.SelectedItem.Text;
            }
            if (Drop_BanZu.SelectedItem != null)
            {
                Session["GroupID"] = Drop_ZuoYeQu.SelectedValue;
                Session["GroupName"] = Drop_ZuoYeQu.SelectedItem.Text;
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/staff_lessons_list.aspx";
            Response.Redirect(url);
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/cpntraining_lessons_list.aspx";
            Response.Redirect(url);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.LoadRep(this.GetStrWhere());
        }

        //绑定Rep控件
        public void LoadRep(string strwhere, int page)
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.StaffLessonsLog().ViewGetCount(strwhere);
            if (page > 0) this.AspNetPager1.CurrentPageIndex = page;
            DataTable dt_PurchaseInfo = new Catering_Rules.StaffLessonsLog().ViewGetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, strwhere, "").Tables[0];
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();
        }

        //绑定Rep控件
        public void LoadRep(string strwhere)
        {
            LoadRep(strwhere, 1);
        }

        //获取查询条件
        public string GetStrWhere()
        {
            StringBuilder sb = new StringBuilder();
            if (this.Drop_BanZu.SelectedValue != "")
            {
                sb.Append(" and S.ClassID =" + this.Drop_BanZu.SelectedValue + " ");
            }

            if (string.IsNullOrEmpty(this.txt_BookName.Text.Trim()))
            {
                if (this.Drop_SearchType.SelectedValue == "BookName")
                {
                    sb.Append(" and N.LessonName like '%" + this.txt_BookName.Text.Trim() + "%' ");
                }
                else if (this.Drop_SearchType.SelectedValue == "BookType")
                {

                }
            }
            return sb.ToString();
        }

        public string GetClassName(string strClassID)
        {
            Catering_Model.ClassInfo _ClassModel = new Catering_Rules.ClassInfo().GetModel(int.Parse(strClassID));
            if (_ClassModel == null) return "";
            return _ClassModel.CName;
        }

        public string GetNodeName(string strNodeID)
        {
            Catering_Model.BranchPost _LessonTypeModel = new Catering_Rules.BranchPost().GetModel(int.Parse(strNodeID));
            if (_LessonTypeModel == null) return "";
            return _LessonTypeModel.Name;
        }

        public string IsGood(string TestPoints)
        {
            if (!string.IsNullOrEmpty(TestPoints))
            {
                return Convert.ToDecimal(TestPoints) >= 60 ? "合格" : "不合格";
            }
            return "";
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep(this.GetStrWhere());
        }
    }
}